//
//  ZZVisitorLoginView.swift
//  sina
//
//  Created by 张鹏 on 16/5/22.
//  Copyright © 2016年 ZP. All rights reserved.
//

import UIKit

    //TODO: 导入头文件.  不在同一个命名空间下的文件之间不可以相互调用
import SnapKit


        //1. 声明协议
        // NSObjectProtocol 继承基协议  可以使用weak 关键字
@objc protocol ZZVisitorLoginViewDelegate : NSObjectProtocol {
        //声明协议方法
    
        //用户的登陆的协议方法
        optional func userWilllogin()
        //用户注册的协议方法
        func userWiilRegister()

}

class ZZVisitorLoginView: UIView {
    

        //2. 声明代理方法
        //代理对象 必须使用弱引用  在swift属性的默认就是强引用
   weak var visitorDelegate : ZZVisitorLoginViewDelegate?
    
        //MARK: 通过外界调用设置访客视图的页面信息
    func updateUI(title :String, imageName: String, isHome: Bool = false) {
        if isHome {

            stratAnimation()
    
        } else {
            
        largeHouse.hidden = true
            
            bringSubviewToFront(circleIcon)
        }
        circleIcon.image = UIImage(named: imageName)
        tipLable.text = title

    }
    
        //系统自定义的自动布局
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //调用设置UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
        //给圈圈设置动画效果
    func stratAnimation() {
            
    let basicAnimation = CABasicAnimation(keyPath: "transform.rotation")
    
        //动画的持续时间
        basicAnimation.duration = 10
        //动画重复次数  MAXFLOAT:无限次
        basicAnimation.repeatCount = MAXFLOAT
        //动画转动的半径
        basicAnimation.toValue = M_PI * 2
        
        //当动画结束的时候,或者处于非活跃状态时,或者切换控制器之后,动画不会移除出图层
        basicAnimation.removedOnCompletion = false
    
        //给视图图层添加动画效果
        circleIcon.layer.addAnimation(basicAnimation, forKey: nil)
    
    }
    
   
    //设置UI
   private  func setupUI() {
    
    //设置背景色
    let colorNum : CGFloat = 237.0/255.0
    self.backgroundColor = UIColor(red: colorNum, green: colorNum, blue: colorNum, alpha: 1)
    
    
    
    
     // view1.attr1 = view2.attr2 * multiplier + constant
    
     //TODO: swift系统原生的约束设置方法
    
//    //设置circleIcon的约束
//    circleIcon.translatesAutoresizingMaskIntoConstraints = false
//    self.addConstraint( NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//    self.addConstraint( NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//    
//    //设置largeHouse的约束
//    largeHouse.translatesAutoresizingMaskIntoConstraints = false
//    self.addConstraint( NSLayoutConstraint(item: largeHouse, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//    self.addConstraint( NSLayoutConstraint(item: largeHouse, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
    
        addSubview(circleIcon)
        addSubview(largeHouse)
        addSubview(backIcon)
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
    
    
    circleIcon.snp_makeConstraints { (make) in
        
        make.centerX.equalTo(snp_centerX)
         make.centerY.equalTo(snp_centerY).offset(-80)
    }
    
    largeHouse.snp_makeConstraints { (make) in
        
        make.centerX.equalTo(snp_centerX)
        make.centerY.equalTo(snp_centerY).offset(-80)
    }
    
    backIcon.snp_makeConstraints { (make) in
        
        make.centerX.equalTo(snp_centerX)
        make.centerY.equalTo(snp_centerY).offset(-80)
    }
    
    tipLable.snp_makeConstraints { (make) in
        make.centerX.equalTo(largeHouse.snp_centerX)
        make.top.equalTo(largeHouse.snp_bottom).offset(80)
    }
    
    loginBtn.snp_makeConstraints { (make) in
        
        make.leftMargin.equalTo(tipLable.snp_leftMargin)
        make.top.equalTo(tipLable.snp_bottom).offset(80)
        make.width.equalTo(100)
    }
    
    registerBtn.snp_makeConstraints { (make) in
        
        make.rightMargin.equalTo(tipLable.snp_rightMargin)
        make.top.equalTo(tipLable.snp_bottom).offset(80)
        make.width.equalTo(100)
    }

    //TODO: 给按钮添加点击事件
    loginBtn.addTarget(self, action: #selector(ZZVisitorLoginView.loginBtnDidClick), forControlEvents: .TouchUpInside)
    
    registerBtn.addTarget(self, action: #selector(ZZVisitorLoginView.registerBtnDidClick), forControlEvents: .TouchUpInside)
    
    }
    
    //按钮的点击事件
    @objc func loginBtnDidClick() {
    
        //自定义的代理方法
        visitorDelegate?.userWilllogin?()

    }
    
   @objc func registerBtnDidClick() {
    
    visitorDelegate?.userWiilRegister()
    
    }
    
   
    
    
    //懒加载控件
    
    //遮罩
    private lazy var backIcon : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //圈圈视图
    private lazy var circleIcon : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon" ))
    
    //大房子视图
    private lazy var largeHouse : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house" ))
    
    //文本视图
    private lazy var tipLable : UILabel = {
    
    let l = UILabel()

        l.text = "关注一些人,回到这里看看有什么惊喜"
        l.font = UIFont.systemFontOfSize(14)
        //设置居中
        l.textAlignment = .Center
        
        //布局最大宽度
        l.preferredMaxLayoutWidth = 300
        //自动换行
        l.numberOfLines = 0
        //自适应大小
        l.sizeToFit()
        
        return l
    }()
    
    //左边按钮
    private lazy var loginBtn : UIButton = {
    
    let btn = UIButton()
    
        //设置按钮文字
        btn.setTitle("登录", forState: .Normal)
        //设置按钮文字颜色
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        //设置按钮文字大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        //剪切图片

        //设置背景颜色
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        
    return btn
    }()
    
    //右边按钮
    private lazy var registerBtn : UIButton = {
        
        let btn = UIButton()
        
        //设置按钮文字
        btn.setTitle("注册", forState: .Normal)
        //设置按钮文字颜色
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Highlighted)
        //设置按钮文字大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        //设置背景颜色
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        
        return btn
    }()

}
